package admin.controllers;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import search.OrderSearchData;

@SuppressWarnings("serial")
public class ListOrderServlet extends HttpServlet {

	private static final Logger log = Logger.getLogger(ListOrderServlet.class
			.getName());

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		process(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			int page = 1;
			try {
				page = Integer.parseInt((String) request.getParameter("page"));
			} catch (Exception e) {
				page = 1;
			}
			String strCity = request.getParameter("city");
			String strDistrict = request.getParameter("district");
			String strOrderId = request.getParameter("orderId");
			String strEmail = request.getParameter("email");
			String strShipName = request.getParameter("shipName");
			String strShipAddress = request.getParameter("shipAddress");
			String strShipPhoneNumber = request.getParameter("shipPhoneNumber");
			String strStartTime = request.getParameter("startTime");
			String strEndTime = request.getParameter("endTime");
			String strStatus = request.getParameter("status");
			String strPaymentType = request.getParameter("paymentType");
			String strCheckOut = request.getParameter("checkOut");

			Date startTime;
			Date endTime;
			int status;
			int paymentType;
			DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
			try {
				startTime = formatter.parse(strStartTime);
				request.setAttribute("startTime", strStartTime);
			} catch (Exception e) {
				startTime = null;
			}
			try {
				endTime = formatter.parse(strEndTime);
				request.setAttribute("endTime", strEndTime);
			} catch (Exception e) {
				endTime = null;
			}
			try {
				status = Integer.parseInt(strStatus);
				request.setAttribute("status", strStatus);
			} catch (Exception e) {
				status = 0;
			}
			try {
				paymentType = Integer.parseInt(strPaymentType);
				request.setAttribute("paymentType", strPaymentType);
			} catch (Exception e) {
				paymentType = 0;
			}			
			if(strCheckOut!=null&&strCheckOut.length()>0){
				request.setAttribute("checkOut", strCheckOut);
			}
			OrderSearchData orderSearch = new OrderSearchData();
			orderSearch.setPage(page);
			if (strOrderId != null && strOrderId.length() > 0) {
				orderSearch.searchById(strOrderId);
				request.setAttribute("orderId", strOrderId);
			} else {
				orderSearch.searchAdvance(null, strCity, strDistrict, strEmail,
						paymentType, strShipName, strShipAddress,
						strShipPhoneNumber, startTime, endTime, status, strCheckOut);
				request.setAttribute("city", strCity);
				request.setAttribute("district", strDistrict);
				request.setAttribute("email", strEmail);
				request.setAttribute("shipName", strShipName);
				request.setAttribute("shipAddress", strShipAddress);
				request.setAttribute("shipPhoneNumber", strShipPhoneNumber);
			}

			request.setAttribute("orderSearch", orderSearch);
			request.setAttribute("url", request.getRequestURL().toString());
			request.getRequestDispatcher("/admin/pages/list_order.jsp")
					.forward(request, response);
		} catch (Exception e) {
			System.out.println("Error when get list Order !");
			log.warning("Error when get list orders !");
			log.warning(e.toString());
			e.printStackTrace();
		}
	}
}
